﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using AIMLbot;

namespace GaduBot
{
    class Responder
    {
        private Bot botek;
        private bool initialized = false;
        private Hashtable UserTable = new Hashtable(100);

        public Responder()
        {
            if (!this.initialized)
            {
                botek = new Bot();
                botek.isAcceptingUserInput = false;
                botek.loadSettings("configs\\Settings.xml");
                botek.loadAIMLFromFiles();
                this.initialized = true;
                this.botek.isAcceptingUserInput = true;
            }
        }
        ~Responder()
        {

        }

        public User GetUser(int uin)
        {
            User ThisUser;

            if (this.UserTable.Contains(uin))
            {
                ThisUser = (User)this.UserTable[uin];
            }
            else
            {
                ThisUser = new User(uin.ToString(), botek);
                this.UserTable.Add(uin, ThisUser);
            }

            if (this.UserTable.Count >= 100)
            {
                throw new OutOfMemoryException();
            }

            return ThisUser;
        }
        public void SaveUser(int uin,User user)
        {
            if (this.UserTable.Contains(uin))
            {
                this.UserTable[uin] = user;
            }
            else
            {
                throw new ArgumentException("Nie ma takiego uzytkownika.");
            }

            if (this.UserTable.Count >= 100)
            {
                throw new OutOfMemoryException("Liczba uzytkownikow przekroczyla maksymalna pamiec bota.");
            }
        }
        public string Respond(string Text,int uin)
        {
            try
            {
                User ThisUser = this.GetUser(uin);
                Request Req = new Request(Text, ThisUser, botek);
                Result res = this.botek.Chat(Req);
                this.SaveUser(uin, ThisUser);
                return res.Output;
            }
            catch (Exception)
            {
                return "BOT ERROR!!";
                throw;
            }

        }
    }
}